package ro.msg.quiz.controller;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ro.msg.quiz.domain.MultipleChoiceQuestion;
import ro.msg.quiz.domain.Question;
import ro.msg.quiz.domain.Quiz;
import ro.msg.quiz.domain.SingleChoiceQuestion;
import ro.msg.quiz.domain.TextQuestion;
import ro.msg.quiz.repository.Repository;

//AUTHOR: Patricia

//Class responsible for getting data from xml file and overwriting data in repository
public class ImportXML {
    private Repository repo = Repository.getInstance();

    public ImportXML() {
	readQuizzes();
	readQuestion();
    }

    public void readQuestion() {

	try {
	    ArrayList<Question> questionList = new ArrayList<Question>();
	    File fXmlFile = new File("questionnaire.xml");
	    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	    Document doc = dBuilder.parse(fXmlFile);
	    doc.getDocumentElement().normalize();
	    NodeList n = doc.getElementsByTagName("Questions");
	    NodeList nList = n.item(0).getChildNodes();
	    for (int temp = 0; temp < nList.getLength(); temp++) {
		Node nNode = nList.item(temp);
		Question q = null;
		if (nNode.getNodeType() == Node.ELEMENT_NODE) {
		    Element eElement = (Element) nNode;
		    int id = Integer.parseInt(eElement.getElementsByTagName("id").item(0).getTextContent());
		    String text = eElement.getElementsByTagName("text").item(0).getTextContent();
		    if (eElement.getElementsByTagName("type").item(0).getTextContent().equals("TEXT_Q")) {
			String answer = eElement.getElementsByTagName("answer").item(0).getTextContent();
			q = new TextQuestion(id, text, answer);
		    } else {
			ArrayList<String> options = new ArrayList<String>();
			for (int i = 0; i < eElement.getElementsByTagName("option").getLength(); i++) {
			    options.add(eElement.getElementsByTagName("option").item(i).getTextContent());
			}
			if (eElement.getElementsByTagName("type").item(0).getTextContent().equals("MULTIPLE_CHOICE_Q")) {
			    ArrayList<Integer> correctIndexes = new ArrayList<Integer>();
			    for (int i = 0; i < eElement.getElementsByTagName("correctIndexes").getLength(); i++) {
				correctIndexes.add(Integer.parseInt(eElement.getElementsByTagName("index").item(i).getTextContent()));
			    }
			    q = new MultipleChoiceQuestion(id, text, options, correctIndexes);
			} else {
			    int correctIndex = Integer.parseInt(eElement.getElementsByTagName("correctIndex").item(0).getTextContent());
			    q = new SingleChoiceQuestion(id, text, options, correctIndex);
			}
		    }

		}
		questionList.add(q);
	    }
	    repo.setQuestionList(questionList);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public void readQuizzes() {
	try {
	    ArrayList<Quiz> quizzeslist = new ArrayList<Quiz>();
	    File fXmlFile = new File("questionnaire.xml");
	    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	    Document doc = dBuilder.parse(fXmlFile);
	    doc.getDocumentElement().normalize();
	    NodeList nList = doc.getElementsByTagName("quizz");
	    NodeList tempList = doc.getElementsByTagName("questions");
	    for (int temp = 0; temp < nList.getLength(); temp++) {
		Node nNode = nList.item(temp);
		if (nNode.getNodeType() == Node.ELEMENT_NODE) {
		    Element eElement = (Element) nNode;
		    int quizzId = Integer.parseInt(eElement.getElementsByTagName("quizzId").item(0).getTextContent());
		    String name = eElement.getElementsByTagName("name").item(0).getTextContent();
		    Quiz quiz = new Quiz(quizzId, name);
		    NodeList nList2 = tempList.item(temp).getChildNodes();
		    for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
			Node nNode2 = nList2.item(temp2);
			Question q = null;
			if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
			    Element eElement2 = (Element) nNode2;
			    int questionId = Integer.parseInt(eElement2.getElementsByTagName("id").item(0).getTextContent());
			    String questionText = eElement2.getElementsByTagName("text").item(0).getTextContent();
			    if (eElement2.getElementsByTagName("type").item(0).getTextContent().equals("TEXT_Q")) {
				String answer = eElement2.getElementsByTagName("answer").item(0).getTextContent();
				q = new TextQuestion(questionId, questionText, answer);
			    } else {
				ArrayList<String> options = new ArrayList<String>();
				for (int i = 0; i < eElement2.getElementsByTagName("option").getLength(); i++) {
				    options.add(eElement2.getElementsByTagName("option").item(i).getTextContent());
				}
				if (eElement2.getElementsByTagName("type").item(0).getTextContent().equals("MULTIPLE_CHOICE_Q")) {
				    ArrayList<Integer> correctIndexes = new ArrayList<Integer>();
				    for (int i = 0; i < eElement2.getElementsByTagName("correctIndex").getLength(); i++) {
					correctIndexes.add(Integer.parseInt(eElement2.getElementsByTagName("correctIndex").item(i).getTextContent()));
				    }
				    q = new MultipleChoiceQuestion(questionId, questionText, options, correctIndexes);
				} else {
				    int correctIndex = Integer.parseInt(eElement2.getElementsByTagName("correctIndex").item(0).getTextContent());
				    q = new SingleChoiceQuestion(questionId, questionText, options, correctIndex);
				}
			    }
			}
			quiz.addQuestion(q);
		    }

		    quizzeslist.add(quiz);
		}
	    }
	    repo.setQuizzesList(quizzeslist);
	} catch (Exception e) {
	    System.err.println("ERR" + e.getMessage().toString());
	}
    }
}
